package com.example.demo.config;

import com.example.demo.entity.*;
import com.example.demo.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private LabRepository labRepository;

    @Autowired
    private LabCategoryRepository labCategoryRepository;

    @Autowired
    private TimeSlotRepository timeSlotRepository;

    @Autowired
    private ReservationRepository reservationRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) {
        // 初始化用户数据
        if (userRepository.count() == 0) {
            User student1 = new User();
            student1.setUsername("student1");
            student1.setPassword(passwordEncoder.encode("123456"));
            student1.setUserType(UserType.STUDENT);
            student1.setName("学生1");
            student1.setStudentId("2024001");
            student1.setCollege("计算机学院");
            student1.setPhone("13800000001");

            User student2 = new User();
            student2.setUsername("student2");
            student2.setPassword(passwordEncoder.encode("123456"));
            student2.setUserType(UserType.STUDENT);
            student2.setName("学生2");
            student2.setStudentId("2024002");
            student2.setCollege("计算机学院");
            student2.setPhone("13800000002");

            User teacher1 = new User();
            teacher1.setUsername("teacher1");
            teacher1.setPassword(passwordEncoder.encode("123456"));
            teacher1.setUserType(UserType.TEACHER);
            teacher1.setName("教师1");
            teacher1.setTeacherId("T2024001");
            teacher1.setCollege("计算机学院");
            teacher1.setPhone("13800000003");

            User teacher2 = new User();
            teacher2.setUsername("teacher2");
            teacher2.setPassword(passwordEncoder.encode("123456"));
            teacher2.setUserType(UserType.TEACHER);
            teacher2.setName("教师2");
            teacher2.setTeacherId("T2024002");
            teacher2.setCollege("计算机学院");
            teacher2.setPhone("13800000004");

            userRepository.saveAll(Arrays.asList(student1, student2, teacher1, teacher2));
        }

        // 初始化实验室分类
        if (labCategoryRepository.count() == 0) {
            LabCategory category1 = new LabCategory();
            category1.setName("计算机实验室");
            category1.setDescription("用于计算机相关实验");

            LabCategory category2 = new LabCategory();
            category2.setName("物理实验室");
            category2.setDescription("用于物理相关实验");

            labCategoryRepository.saveAll(Arrays.asList(category1, category2));
        }

        // 初始化实验室
        if (labRepository.count() == 0) {
            LabCategory computerLab = labCategoryRepository.findByName("计算机实验室").get();
            LabCategory physicsLab = labCategoryRepository.findByName("物理实验室").get();

            Lab lab1 = new Lab();
            lab1.setName("计算机实验室1");
            lab1.setCategory(computerLab);
            lab1.setCapacity(50);
            lab1.setLocation("教学楼A101");

            Lab lab2 = new Lab();
            lab2.setName("计算机实验室2");
            lab2.setCategory(computerLab);
            lab2.setCapacity(50);
            lab2.setLocation("教学楼A102");

            Lab lab3 = new Lab();
            lab3.setName("物理实验室1");
            lab3.setCategory(physicsLab);
            lab3.setCapacity(50);
            lab3.setLocation("教学楼B101");

            TimeSlot slot1 = new TimeSlot();
            slot1.setStartTime(LocalTime.of(8, 0));
            slot1.setEndTime(LocalTime.of(10, 0));
            slot1.setName("上午第一节");
            slot1.setOrderNum(1);
            lab1.getAvailableTimeSlots().add(slot1);

            TimeSlot slot2 = new TimeSlot();
            slot2.setStartTime(LocalTime.of(10, 0));
            slot2.setEndTime(LocalTime.of(12, 0));
            slot2.setName("上午第二节");
            slot2.setOrderNum(2);
            lab2.getAvailableTimeSlots().add(slot2);

            TimeSlot slot3 = new TimeSlot();
            slot3.setStartTime(LocalTime.of(14, 0));
            slot3.setEndTime(LocalTime.of(16, 0));
            slot3.setName("下午第一节");
            slot3.setOrderNum(3);
            lab3.getAvailableTimeSlots().add(slot3);

            timeSlotRepository.saveAll(Arrays.asList(slot1, slot2, slot3));
            labRepository.saveAll(Arrays.asList(lab1, lab2, lab3));
        }
    }
} 